package com.yunhe.authority.service.authority.impl;

import com.yunhe.authority.domain.authority.Station;
import com.yunhe.authority.domain.authority.StationStaticData;
import com.yunhe.authority.repository.authority.StationRepository;
import com.yunhe.authority.repository.authority.StationStaticDataRepository;
import com.yunhe.authority.service.authority.StationStaticDataService;
import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.exception.ObjectNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

/**
 * @author zhengbo
 * @date 2020/11/3 14:47
 */
@Service
public class StationStaticDataServiceImpl implements StationStaticDataService {

    @Autowired
    private StationStaticDataRepository stationStaticDataRepository;

    @Autowired
    private StationRepository stationRepository;

    @Override
    @Transactional(rollbackFor = Exception.class, transactionManager = "transactionManager")
    public StationStaticData creatStationStaticData(Long stationId, StationStaticData stationStaticData) throws ObjectNotFoundException, ArgumentErrorException {
        findStationById(stationId);

        //还得做个唯一性判断
        StationStaticData existStationStaticData = stationStaticDataRepository.findByStationId(stationId);
        if (existStationStaticData != null) {
            throw new ArgumentErrorException("此电站已存在静态数据，请勿重复添加");
        }

        StationStaticData newStationStaticData = stationStaticDataRepository.save(stationStaticData);
        //绑定电站
        stationStaticDataRepository.bindStationById(stationId, newStationStaticData.getId());
        return newStationStaticData;
    }
    @Override
    @Transactional(rollbackFor = Exception.class, transactionManager = "transactionManager")
    public StationStaticData updateStationStaticDataByStationId(Long stationId, StationStaticData stationStaticData) throws ObjectNotFoundException {
        findStationById(stationId);

        StationStaticData newStationStaticData = stationStaticDataRepository.save(stationStaticData);
        return newStationStaticData;
    }

    @Override
    @Transactional(rollbackFor = Exception.class, transactionManager = "transactionManager")
    public StationStaticData updateStationStaticData(Long stationId,
                                                     Long id,
                                                     Double area,
                                                     Double taxes,
                                                     Long ssCompNum,
                                                     Long intendSsCompNum,
                                                     Long allOfficeNum,
                                                     Long solvingEmployment,
                                                     Long electromechanicalCompNum,
                                                     Long packingCompNum,
                                                     Long materialsCompNum,
                                                     Long automotiveCompNum,
                                                     Long foodCompNum,
                                                     Long otherCompNum,
                                                     Long industryNum,
                                                     String[] compStarListMonth,
                                                     String[] compStarListYear,
                                                     Double aouCompNumAbove3000,
                                                     Double aouCompNumAbove5000,
                                                     Double aouCompNumAbove10000,
                                                     Long mediumAlarmNum,
                                                     Long mediumAlarmProcessingRate,
                                                     Long heaviestAlarmNum,
                                                     Long heaviestAlarmProcessingRate,
                                                     Double waterConsumptionDay,
                                                     Double waterConsumptionMonth,
                                                     Double waterConsumptionYear,
                                                     String[] waterConsumptionDayList,
                                                     String[] waterConsumptionMonthList,
                                                     String[] waterConsumptionYearList,
                                                     Double sharpElectricityDay,
                                                     Double peakElectricityDay,
                                                     Double flatElectricityDay,
                                                     Double valleyElectricityDay,
                                                     Double sharpElectricityMonth,
                                                     Double peakElectricityMonth,
                                                     Double flatElectricityMonth,
                                                     Double valleyElectricityMonth,
                                                     Double sharpElectricityYear,
                                                     Double peakElectricityYear,
                                                     Double flatElectricityYear,
                                                     Double valleyElectricityYear,
                                                     Double smartDevicesFeesDay,
                                                     Double materialsFeesDay,
                                                     Double environmentFeesDay,
                                                     Double smartDevicesFeesMonth,
                                                     Double materialsFeesMonth,
                                                     Double environmentFeesMonth,
                                                     Double smartDevicesFeesYear,
                                                     Double materialsFeesYear,
                                                     Double environmentFeesYear,
                                                     String[] alarmList,
                                                     String[] loadForecastListDay,
                                                     String[] loadForecastListMonth,
                                                     String[] loadForecastListYear) throws ObjectNotFoundException {

        findStationById(stationId);
        Optional<StationStaticData> optionalStationStaticData = stationStaticDataRepository.findById(id);
        if (!optionalStationStaticData.isPresent()) {
            throw  new ObjectNotFoundException("未查到企业静态数据");
        }
        StationStaticData stationStaticData = optionalStationStaticData.get();
        Optional.ofNullable(area).ifPresent(stationStaticData::setArea);
        Optional.ofNullable(taxes).ifPresent(stationStaticData::setTaxes);
        Optional.ofNullable(ssCompNum).ifPresent(stationStaticData::setSsCompNum);
        Optional.ofNullable(intendSsCompNum).ifPresent(stationStaticData::setIntendSsCompNum);
        Optional.ofNullable(allOfficeNum).ifPresent(stationStaticData::setAllOfficeNum);
        Optional.ofNullable(solvingEmployment).ifPresent(stationStaticData::setSolvingEmployment);
        Optional.ofNullable(electromechanicalCompNum).ifPresent(stationStaticData::setElectromechanicalCompNum);
        Optional.ofNullable(packingCompNum).ifPresent(stationStaticData::setPackingCompNum);
        Optional.ofNullable(materialsCompNum).ifPresent(stationStaticData::setMaterialsCompNum);
        Optional.ofNullable(automotiveCompNum).ifPresent(stationStaticData::setAutomotiveCompNum);
        Optional.ofNullable(foodCompNum).ifPresent(stationStaticData::setFoodCompNum);
        Optional.ofNullable(otherCompNum).ifPresent(stationStaticData::setOtherCompNum);
        Optional.ofNullable(industryNum).ifPresent(stationStaticData::setIndustryNum);
        Optional.ofNullable(compStarListMonth).ifPresent(stationStaticData::setCompStarListMonth);
        Optional.ofNullable(compStarListYear).ifPresent(stationStaticData::setCompStarListYear);
        Optional.ofNullable(aouCompNumAbove3000).ifPresent(stationStaticData::setAouCompNumAbove3000);
        Optional.ofNullable(aouCompNumAbove5000).ifPresent(stationStaticData::setAouCompNumAbove5000);
        Optional.ofNullable(aouCompNumAbove10000).ifPresent(stationStaticData::setAouCompNumAbove10000);
        Optional.ofNullable(mediumAlarmNum).ifPresent(stationStaticData::setMediumAlarmNum);
        Optional.ofNullable(mediumAlarmProcessingRate).ifPresent(stationStaticData::setMediumAlarmProcessingRate);
        Optional.ofNullable(heaviestAlarmNum).ifPresent(stationStaticData::setHeaviestAlarmNum);
        Optional.ofNullable(heaviestAlarmProcessingRate).ifPresent(stationStaticData::setHeaviestAlarmProcessingRate);
        Optional.ofNullable(waterConsumptionDay).ifPresent(stationStaticData::setWaterConsumptionDay);
        Optional.ofNullable(waterConsumptionMonth).ifPresent(stationStaticData::setWaterConsumptionMonth);
        Optional.ofNullable(waterConsumptionYear).ifPresent(stationStaticData::setWaterConsumptionYear);
        Optional.ofNullable(waterConsumptionDayList).ifPresent(stationStaticData::setWaterConsumptionDayList);
        Optional.ofNullable(waterConsumptionMonthList).ifPresent(stationStaticData::setWaterConsumptionMonthList);
        Optional.ofNullable(waterConsumptionYearList).ifPresent(stationStaticData::setWaterConsumptionYearList);
        Optional.ofNullable(sharpElectricityDay).ifPresent(stationStaticData::setSharpElectricityDay);
        Optional.ofNullable(peakElectricityDay).ifPresent(stationStaticData::setPeakElectricityDay);
        Optional.ofNullable(flatElectricityDay).ifPresent(stationStaticData::setFlatElectricityDay);
        Optional.ofNullable(valleyElectricityDay).ifPresent(stationStaticData::setValleyElectricityDay);
        Optional.ofNullable(sharpElectricityMonth).ifPresent(stationStaticData::setSharpElectricityMonth);
        Optional.ofNullable(peakElectricityMonth).ifPresent(stationStaticData::setPeakElectricityMonth);
        Optional.ofNullable(flatElectricityMonth).ifPresent(stationStaticData::setFlatElectricityMonth);
        Optional.ofNullable(valleyElectricityMonth).ifPresent(stationStaticData::setValleyElectricityMonth);
        Optional.ofNullable(sharpElectricityYear).ifPresent(stationStaticData::setSharpElectricityYear);
        Optional.ofNullable(peakElectricityYear).ifPresent(stationStaticData::setPeakElectricityYear);
        Optional.ofNullable(flatElectricityYear).ifPresent(stationStaticData::setFlatElectricityYear);
        Optional.ofNullable(valleyElectricityYear).ifPresent(stationStaticData::setValleyElectricityYear);
        Optional.ofNullable(smartDevicesFeesDay).ifPresent(stationStaticData::setSmartDevicesFeesDay);
        Optional.ofNullable(materialsFeesDay).ifPresent(stationStaticData::setMaterialsFeesDay);
        Optional.ofNullable(environmentFeesDay).ifPresent(stationStaticData::setEnvironmentFeesDay);
        Optional.ofNullable(smartDevicesFeesMonth).ifPresent(stationStaticData::setSmartDevicesFeesMonth);
        Optional.ofNullable(materialsFeesMonth).ifPresent(stationStaticData::setMaterialsFeesMonth);
        Optional.ofNullable(environmentFeesMonth).ifPresent(stationStaticData::setEnvironmentFeesMonth);
        Optional.ofNullable(smartDevicesFeesYear).ifPresent(stationStaticData::setSmartDevicesFeesYear);
        Optional.ofNullable(materialsFeesYear).ifPresent(stationStaticData::setMaterialsFeesYear);
        Optional.ofNullable(environmentFeesYear).ifPresent(stationStaticData::setEnvironmentFeesYear);
        Optional.ofNullable(alarmList).ifPresent(stationStaticData::setAlarmList);
        Optional.ofNullable(loadForecastListDay).ifPresent(stationStaticData::setLoadForecastListDay);
        Optional.ofNullable(loadForecastListMonth).ifPresent(stationStaticData::setLoadForecastListMonth);
        Optional.ofNullable(loadForecastListYear).ifPresent(stationStaticData::setLoadForecastListYear);
        stationStaticData  = stationStaticDataRepository.save(stationStaticData);
        return stationStaticData;
    }

    @Override
    public StationStaticData findByStationId(Long stationId) {
        return stationStaticDataRepository.findByStationId(stationId);
    }

    private void findStationById(Long stationId) throws ObjectNotFoundException {
        Optional<Station> station = stationRepository.findById(stationId);
        if (!station.isPresent()) {
            throw new ObjectNotFoundException("电站不存在");
        }
    }
}
